package com.bingo.mpa.portal;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.express.portal.HttpUtils;
import org.express.portal.Mapping;
import org.express.portal.persist.QueryHelper;
import org.express.portal.renderer.RedirectRenderer;
import org.express.portal.renderer.Renderer;
import org.express.portal.renderer.TemplateRenderer;
import org.express.portal.renderer.TextRenderer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bingo.mpa.util.Common;
import com.bingo.mpa.util.Tools;

public class Operate
{
	private final Logger log = LoggerFactory.getLogger(getClass());
	
	@Mapping("/operate/order")
	public Renderer operate_order()
	{
		Map<String,Object> model = new HashMap<String,Object>();
		try
		{
			String token = HttpUtils.getCookie("mpa_token");
			String sql = "SELECT * FROM P_USER WHERE TOKEN=?";
			if(token == null)
			{
				return new RedirectRenderer("/login");
			}
			com.bingo.mpa.pojo.User user = QueryHelper.read_cache(com.bingo.mpa.pojo.User.class, "user", token, sql, new Object[]{token});
			if(user == null)
			{
				return new RedirectRenderer("/login");
			}
			byte role = user.getRole();
			switch(role)
			{
				case 1:
				case 3:
				case 4:
					return new RedirectRenderer("/login");
			}
			
			long aid = HttpUtils.getLongParameter("aid", -1);
			int status = HttpUtils.getIntParameter("status", -1);
			String dateType = HttpUtils.getStringParameter("dateType");
			String st = HttpUtils.getStringParameter("st");
			String et = HttpUtils.getStringParameter("et");
			if(dateType == null)
			{
				dateType = "today";
				st = Common.getMetaDate(dateType).getSt();
				et = Common.getMetaDate(dateType).getEt();
			}
			String keyword = HttpUtils.getStringParameter("keyword");
			keyword = Tools.replaceStr(keyword);
			StringBuffer sqll = new StringBuffer("SELECT COUNT(o.OID) FROM P_STORE s,P_ORDER o WHERE s.SID=o.SID");
			List<Object> ll = new ArrayList<Object>();
			if(aid > 0)
			{
				sqll.append(" AND o.AID="+aid);
			}
			if(status >0)
			{
				sqll.append(" AND o.STATUS="+status);
			}
			if(st != null && st.length() > 0)
			{
				sqll.append(" AND o.ORDER_TIME >=?");
				java.util.Date _st = org.apache.commons.lang.time.DateUtils.parseDate(st, new String[]{"yyyy-MM-dd"});
				ll.add(_st);
			}
			if(et != null && et.length() > 0)
			{
				sqll.append(" AND o.ORDER_TIME <?");
				java.util.Date _et = org.apache.commons.lang.time.DateUtils.parseDate(et, new String[]{"yyyy-MM-dd"});
				ll.add(_et);
			}
			if(keyword != null && keyword.length()>0)
			{
				sqll.append(" AND (o.PHONE LIKE '%"+keyword+"%')");
			}
			Object[] param = new Object[ll.size()];
			ll.toArray(param);
			long count = QueryHelper.stat(sqll.toString(), param);
			model.put("keyword", keyword);
			model.put("count", count);
			
			sql = "SELECT * FROM P_AGENT";
			List<com.bingo.mpa.pojo.Agent> list = QueryHelper.query_cache(com.bingo.mpa.pojo.Agent.class, "stat_agents", "stat_agents", sql, new Object[]{});
			model.put("list", list);
			model.put("aid", aid);
			model.put("status", status);
			model.put("dateType", dateType);
			model.put("st", st);
			model.put("et", et);
		}
		catch(Exception e)
		{
			log.error("",e);
		}
		return new TemplateRenderer("/order_operate.html","model",model);
	}
	
	@Mapping("/operate/order/page")
	public Renderer order_page()
	{
		StringBuilder sb = new StringBuilder();
		try
		{
			int page = HttpUtils.getIntParameter("page",1);
			int pageSize = HttpUtils.getIntParameter("pageSize", 9);
			
			long aid = HttpUtils.getLongParameter("aid", -1);
			int status = HttpUtils.getIntParameter("status", -1);
			String st = HttpUtils.getStringParameter("st");
			String et = HttpUtils.getStringParameter("et");
			String keyword = HttpUtils.getStringParameter("keyword");
			keyword = Tools.replaceStr(keyword);
			List<Object> ll = new ArrayList<Object>();
			StringBuffer sql = new StringBuffer("SELECT s.NAME,o.* FROM P_STORE s,P_ORDER o WHERE s.SID=o.SID");
			if(aid > 0)
			{
				sql.append(" AND o.AID="+aid);
			}
			if(status >0)
			{
				sql.append(" AND o.STATUS="+status);
			}
			if(st != null && st.length() > 0)
			{
				sql.append(" AND o.ORDER_TIME >=?");
				java.util.Date _st = org.apache.commons.lang.time.DateUtils.parseDate(st, new String[]{"yyyy-MM-dd"});
				ll.add(_st);
			}
			if(et != null && et.length() > 0)
			{
				sql.append(" AND o.ORDER_TIME <?");
				java.util.Date _et = org.apache.commons.lang.time.DateUtils.parseDate(et, new String[]{"yyyy-MM-dd"});
				ll.add(_et);
			}
			if(keyword != null && keyword.length()>0)
			{
				sql.append(" AND (o.PHONE LIKE '%"+keyword+"%')");
			}
			sql.append(" ORDER BY o.OID DESC");
			Object[] param = new Object[ll.size()];
			ll.toArray(param);
			
			List<com.bingo.mpa.vo.OrderVO> objs = QueryHelper.query_slice(com.bingo.mpa.vo.OrderVO.class, sql.toString(), page, pageSize,param);
			
			sql = new StringBuffer("SELECT * FROM P_PACKAGE");
			List<com.bingo.mpa.pojo.Package> lp = QueryHelper.query_cache(com.bingo.mpa.pojo.Package.class, "packages", "packages", sql.toString(), new Object[]{});
			Map<Integer,com.bingo.mpa.pojo.Package> map = new HashMap<Integer,com.bingo.mpa.pojo.Package>();
			if(lp != null && lp.size() > 0)
			{
				for(com.bingo.mpa.pojo.Package obj : lp)
				{
					map.put(Integer.valueOf(obj.getPid()), obj);
				}
			}
			
			NumberFormat currentFormat=NumberFormat.getCurrencyInstance(Locale.CHINA);
			com.bingo.mpa.pojo.Package tmp = null;
			for(com.bingo.mpa.vo.OrderVO obj :objs)	
			{
				sb.append("<tr><td width=\"110\">");
				sb.append(org.apache.commons.lang.time.DateFormatUtils.format(obj.getOrder_time(), "yyyy/MM/dd HH:mm"));
				sb.append("</td><td width=\"120\">");
				sb.append(obj.getName());
				sb.append("</td><td width=\"30\">");
				sb.append(obj.getS_account());
				sb.append("</td><td class=\"num16\" width=\"100\">");
				sb.append(obj.getPhone().substring(0, 7));
				sb.append("<span class=\"orange\">"+obj.getPhone().substring(7)+"</span>");
				sb.append("</td><td width=\"110\">");
				tmp = map.get(obj.getPackage_());
				if(tmp != null)
				{
					switch(tmp.getCarrier())
					{
						case 1:
							sb.append("<img src=\"/images/icon_mobile.png\"/>");
							break;
						case 2:
							sb.append("<img src=\"/images/icon_unicom.png\"/>");
							break;
					}
					sb.append(tmp.getName());
				}
				sb.append("</td><td width=\"40\">");
				sb.append(currentFormat.format(obj.getRetail()/100.0));
				sb.append("</td><td width=\"40\">");
				sb.append(currentFormat.format(obj.getPrestore()/100.0));
				sb.append("</td><td width=\"40\">");
				sb.append(currentFormat.format(obj.getDiscount()/100.0));
				sb.append("</td><td width=\"40\">");
				sb.append(obj.getStatus()==1?"<span style=\"color:red\">待开卡</span>":obj.getStatus()==2?"<span style=\"color:green\">已开卡</span>":obj.getStatus()==3?"<span style=\"color:blue\">已撤单</span>":obj.getStatus()==4?"<span style=\"color:red\">已驳回</span>":"未知");
				sb.append("</td><td width=\"50\">");
				sb.append(obj.getPay_status()==1?"<span style=\"color:red\">未支付</span>":obj.getPay_status()==2?"<span style=\"color:blue\">处理中</span>":obj.getPay_status()==3?"<span style=\"color:green\">已支付</span>":obj.getPay_status()==4?"<span style=\"color:red\">失败</span>":"未知");
				sb.append("</td><td>");
				sb.append(obj.getRemark()==null?"":obj.getRemark());
				String prestore = "";
				if(obj.getPrestore()>0)
				{
					prestore = "（预存"+obj.getPrestore()/100+"）";
				}
				String pack = tmp != null?tmp.getName():"无";
				sb.append("</td><td width=\"70\"><a href=\"#\" onclick=\"detail("+obj.getOid()+",'"+pack+prestore+"');\">");
				if(obj.getId_card() != null)
				{
					sb.append("操作&nbsp;|<a href=\"/admin/order/d/"+obj.getOid()+"\">&nbsp;附件</a>");
				}
				else
				{
					sb.append("操作&nbsp;|<a href=\"#\">&nbsp;XX</a>");
				}
				sb.append("</td></tr>");
			}
			if(objs.size()<9)
			{
				for(int i = 0;i<9-objs.size();i++)
				{
					sb.append("<tr><td>");
					sb.append("</td><td>");
					sb.append("</td><td>");
					sb.append("</td><td>");
					sb.append("</td><td>");
					sb.append("</td><td>");
					sb.append("</td><td>");
					sb.append("</td><td>");
					sb.append("</td><td>");
					sb.append("</td><td>");
					sb.append("</td><td>");
					sb.append("</td><td>");
					sb.append("</td></tr>");
				}
			}
			log.info(page+","+pageSize);
		}
		catch(Exception e)
		{
			log.error("",e);
		}
		return new TextRenderer(sb.toString());
	}
}
